/**    
 * @Description: 博客园地址：https://www.cnblogs.com/zumengjie/      
 * @author: 顶风少年   
 * @date: 2021年4月4日 下午5:18:15
 */

package datang.dtdb.datasource.unpooled;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

import javax.sql.DataSource;

import datang.dtdb.exceptions.DataSourceException;
import datang.dtdb.io.Resources;

/**
 * 没有连接池，每次都是获取一个新得连接
 */
public class UnpooledDataSource implements DataSource {

	private String url;
	private String username;
	private String password;

	/**
	 * 加载驱动
	 */
	public void setDriverClassName(String driverClassName) {
		try {
			Resources.classForName(driverClassName);
		} catch (ClassNotFoundException e) {
			throw new DataSourceException("加载DriverManager异常", e);
		}
	}

	/**
	 * 设置连接
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * 设置用户名
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * 设置密码
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * 每次都获取一个新的连接
	 */
	@Override
	public Connection getConnection() {
		Connection connection;
		try {
			connection = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			throw new DataSourceException("获取连接异常", e);
		}
		return connection;
	}

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		return null;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		return 0;
	}

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		return null;
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		return null;
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return false;
	}

	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		return null;
	}

}
